package com.global.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.global.dao.DataSourceXDao;
import com.global.model.DataSourceX;

@Repository("dataSourceXDao")
public class DataSourceXDaoImpl extends GenericDaoHibernate<DataSourceX, Long> implements DataSourceXDao {

	public DataSourceXDaoImpl() {
		super(DataSourceX.class);
	}

	/**
	 * {@inheritDoc}
	 */
	public DataSourceX getDataSourceX(String name) {
		if(StringUtils.isNotBlank(name)){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("uniqueName", name);
			List<DataSourceX> list = this.findByNamedQuery("findDataSourceXByName", queryParams);
			if(!list.isEmpty()){
				return list.get(0);
			}
		}

		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public DataSourceX saveDataSourceX(DataSourceX dataSourceX) {
		if(dataSourceX!=null){
			if(log.isDebugEnabled()){
				log.debug("dataSourceX's id: " + dataSourceX.getId());
			}
			getHibernateTemplate().saveOrUpdate(dataSourceX);
			getHibernateTemplate().flush();
		}
		
		return dataSourceX;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public boolean saveBatchDataSourceX(List<DataSourceX> dataSourceXList) {
		if(dataSourceXList!=null){
			getHibernateTemplate().saveOrUpdateAll(dataSourceXList);
			getHibernateTemplate().flush();
			
			return true;
		}
		
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	public void deleteDataSourceX(String name) {
		DataSourceX dataSourceX = getDataSourceX(name);
		if(dataSourceX!=null){
			getHibernateTemplate().delete(dataSourceX);			
			getHibernateTemplate().flush();
		}
	}
}
